package com.lzq.model.adapter;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/22 15:16
 * @updateUser : modify
 * @updateTime : 2022/11/22 15:16
 * @updateRemark : 类适配器模式
 */
public class SdAdapterTf extends TfCardImp implements SdCard {
    @Override
    public String readSD() {
        return readTF();
    }

    @Override
    public void writeSD(String msg) {
        System.out.println("write tf");
        writeTF(msg);
    }
}

class Computer {
    public String readSd(SdCard sdCard) {
        if (sdCard == null) {
            throw new NullPointerException("sd card null");
        }
        return sdCard.readSD();
    }
}

class Test {
    public static void main(String[] args) {
        Computer computer = new Computer();
        String msg = computer.readSd(new SdCardImp());
        System.out.println(msg);
        System.out.println("<<<<<<=========>>>>>");
        String msg1 = computer.readSd(new SdAdapterTf());
        System.out.println(msg1);
    }
}

/**
 * 对象适配器模式
 */
class SdAdapterTfObject implements SdCard {
    private TfCard tfCard;

    public SdAdapterTfObject(TfCard tfCard) {
        this.tfCard = tfCard;
    }

    @Override
    public String readSD() {
        return tfCard.readTF();
    }

    @Override
    public void writeSD(String msg) {
        tfCard.writeTF(msg);
    }
}

class ObjectTest {
    public static void main(String[] args) {
        Computer computer = new Computer();
        SdCard sdCard = new SdCardImp();
        System.out.println(computer.readSd(sdCard));
        System.out.println("<<<<<<======>>>>>");
        SdAdapterTfObject sdAdapterTfObject = new SdAdapterTfObject(new TfCardImp());
        String msg = sdAdapterTfObject.readSD();
        System.out.println(msg);
    }
}